//
//  UIView+frame.m
//  ROL
//
//  Created by  on 2021/2/19.
//  Copyright © 2021 鸣世电竞. All rights reserved.
//

#import "UIView+frame.h"

@implementation UIView (frame)
- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}
- (CGFloat)x
{
    return self.frame.origin.x;
}


- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}
- (CGFloat)y
{
    return self.frame.origin.y;
}


- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}
- (CGFloat)width
{
    return self.frame.size.width;
}


- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}
- (CGFloat)height
{
    return self.frame.size.height;
}


- (void)setCenterX:(CGFloat)centerX
{
    CGPoint point = self.center;
    point.x = centerX;
    self.center = point;
}
- (CGFloat)centerX
{
    return self.center.x;
}


- (void)setCenterY:(CGFloat)centerY
{
    CGPoint point = self.center;
    point.y = centerY;
    self.center = point;
}
- (CGFloat)centerY
{
    return self.center.y;
}


- (void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}
- (CGSize)size
{
    return self.frame.size;
}


- (void)setOrigin:(CGPoint)origin
{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}
- (CGPoint)origin
{
    return self.frame.origin;
}


- (void)setMaxX:(CGFloat)maxX
{
    CGRect frame = self.frame;
    frame.origin.x = maxX - self.width;
    self.frame = frame;
}
- (CGFloat)maxX
{
    return self.x + self.width;
}


- (void)setMaxY:(CGFloat)maxY
{
    self.y = maxY - self.height;
}
- (CGFloat)maxY
{
    return self.y + self.height;
}

-(void)setShowNoDataView:(UILabel *)showNoDataView
{
    objc_setAssociatedObject(self, @selector(showNoDataView), showNoDataView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UILabel *)showNoDataView
{
    UILabel *label = objc_getAssociatedObject(self, @selector(showNoDataView));
    if(label)
        return label;
    else
    {
        label = [[UILabel alloc] init];
        label.text = @"暂无数据";
        label.textColor = ([self.backgroundColor isEqual:[UIColor whiteColor]]) ? [UIColor blackColor] : [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.hidden = YES;
        //        label.backgroundColor = [UIColor redColor];
        [self addSubview:label];
        NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
        NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:0];
        NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0];
        NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
        [self addConstraints:@[left,right,top,bottom]];
        return label;
    }
}

//- (void)removeAllSubView
//{
//    for (UIView *view in [self subviews])
//    {
//        [view removeFromSuperview];
//    }
//}
- (void)ergodicSubView:(NSInteger)tag block:(void (^)(id subview))block
{
    for (UIView *view in [self subviews])
    {
        if (view.tag >= tag)
        {
            if (block)
            {
                block(view);
            }
        }
    }
}

//+ (instancetype)loadViewFromXIB
//{
//    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] firstObject];
//}
//+ (UINib *)nib
//{
//    return [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil];
//}
@end
